package hut.lgl.servlet;

import com.alibaba.fastjson.JSON;
import hut.lgl.bean.Book;
import hut.lgl.service.BookService;
import hut.lgl.utils.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/book")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookService();
    private ResultVo resultVo = null;


    public void deleteBook(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String bid = req.getParameter("bid");
        int i = bookService.deleteBook(Integer.parseInt(bid));
        if (i>0){
            resultVo = new ResultVo(200,"删除成功了",i);
        }else{
            resultVo = new ResultVo(500,"删除失败了",i);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    public void updateBook(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Book book = new Book();
        BeanUtils.populate(book,map);

        int i = bookService.updateBook(book);
        if (i>0){
            resultVo = new ResultVo(200,"修改成功了",i);
        }else{
            resultVo = new ResultVo(500,"修改失败了",i);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    //修改图书信息 前的回显
    public void toShowBook(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String bid = req.getParameter("bid");
        System.out.println(bid);
        Book book = bookService.getBookById(Integer.parseInt(bid));
        resultVo = new ResultVo(200,"成功回显",book);
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    //添加图书
    public void addBook(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        System.out.println(map);
        Book book = new Book();
        BeanUtils.populate(book,map);
        int i = bookService.addBook(book);
        if (i>0){
            resultVo = new ResultVo(200,"添加成功了",i);
        }else{
            resultVo = new ResultVo(500,"添加失败了",i);
        }
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }

    //查询所有图书
    public void getAllBooks(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Book> list = bookService.getAllBooks();
        resultVo = new ResultVo(200,"查询成功了",list);
        String s = JSON.toJSONString(resultVo);
        resp.getWriter().print(s);
    }
}
